using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace OpenComposite.Base
{
	[ToolboxBitmap(typeof(TreeView))]
	public class TreeViewEx : TreeView
	{
		#region Initialization

		public TreeViewEx()
		{
			this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
			this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
			this.SetStyle(ControlStyles.ResizeRedraw, false);
			this.UpdateStyles();
			this.ResizeRedraw = false;
		}

		#endregion

		#region Public Members

		#region Events

		[Category("Action")]
		public event ScrollEventHandler Scroll = null;
		//[Category("Mouse")]
		//public event MouseEventHandler MouseWheel = null;

		#endregion

		#region Properties

		public new TreeViewDrawMode DrawMode
		{
			get { return base.DrawMode; }
			set
			{
				base.DrawMode = value;
				this.SetStyle(ControlStyles.ResizeRedraw, false);
				this.UpdateStyles();
			}
		}

		#endregion

		#endregion

		#region Protected Members

		#region WndProc

		protected override void WndProc(ref Message m)
		{
			base.WndProc(ref m);
			if ( m.Msg == (int)WindowsMessages.WM_VSCROLL ) {
				ScrollEventType type;
				try { 
					type = (ScrollEventType)m.WParam;
				} catch {
					type = ScrollEventType.EndScroll;
				}
				OnScroll(type, (int)m.LParam, ScrollOrientation.VerticalScroll);
			} else if ( m.Msg == (int)WindowsMessages.WM_HSCROLL ) {
				ScrollEventType type;
				try {
					type = (ScrollEventType)m.WParam;
				} catch {
					type = ScrollEventType.EndScroll;
				}
				OnScroll(type, (int)m.LParam, ScrollOrientation.HorizontalScroll);
			} else if ( m.Msg == (int)WindowsMessages.WM_MOUSEWHEEL ) {
			}
		}

		protected override void OnDrawNode(DrawTreeNodeEventArgs e)
		{
			base.OnDrawNode(e);
		}
		#endregion

		#region Methods

		protected void OnScroll(ScrollEventType type, int newValue, ScrollOrientation scroll)
		{
			ScrollEventHandler tmp = this.Scroll;
			if ( tmp != null ) {
				tmp(this, new ScrollEventArgs(type, newValue, scroll));
			}
		}

		#endregion

		#endregion

		#region Private Members
		#endregion
	}
}
